<template>
	<view>
		<view class="BuuetoothServiceList">
			<text>蓝牙设备列表</text>
		</view>
		<button style="width: 9rem;height: 3rem;background-color: rgb(138, 145, 159);"
			@click="getyipeiduideshebeiList">刷新</button>
		<scroll-view scroll-y class="box">
			<view class="item" v-for="item in blueDeviceList" @click="connect(item)">
				<view>
					<text>id: {{ item.deviceId }}</text>
				</view>
				<view>
					<text>地址:{{item.address}}</text>
				</view>
				<view>
					<text>name: {{ item.name }}</text>
				</view>
			</view>
		</scroll-view>
		<button style="width: 9rem;height: 3rem;background-color: rgb(138, 145, 159);"
			@click="getyipeiduideshebeiList">已配对的设备列表</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				blueDeviceList: [],
				deviceId: null,
				sevicesList: [],
				serviceUuid: null,
				eigenvalueList: [],
				eigenvalue: null,
				message: "",
				messageHex: "",
				sendMessage: ""
			}
		},
		methods: {
			/**
			 * 获取当前已连接的设备
			 */
			getyipeiduideshebeiList() {
				const that = this;
				uni.openBluetoothAdapter({
					success(res) {
						this.sevicesList = [];
						uni.showLoading({
							title: '开始获取已配对的设备..'
						})
						const main = plus.android.runtimeMainActivity();
						const Context = plus.android.importClass("android.content.Context");
						const BManager = main.getSystemService(Context.BLUETOOTH_SERVICE);
						plus.android.importClass(BManager); //引入相关的method函数  
						const BAdapter = BManager.getAdapter();
						plus.android.importClass(BAdapter); //引入相关的method函数，这样之后才会有isEna  
						const lists = BAdapter.getBondedDevices();
						plus.android.importClass(lists);
						const iterator = lists.iterator();
						plus.android.importClass(iterator);
						const blueTooths = iterator.hasNext();
						uni.hideLoading();
						while (iterator.hasNext()) {
							const d = iterator.next();
							plus.android.importClass(d);
							console.log("名称：" + d.getName() + "，地址：" + d.getAddress());
							console.log("类型：" + d.getType());
							console.log("，UUIDs：", d.getUuids());
							uni.getConnectedBluetoothDevices({
								services: d.getUuids().map(o => o.__UUID__),
								success(res) {
									console.log('根据 uuid 获取处于已连接状态的设备:', res)
								}
							})

							// --------------------
							const item = {
								name: d.getName(),
								address: d.getAddress(),
								type: d.getType(),
								// deviceId: d.getDeviceId()
								deviceId: ''
							}
							that.blueDeviceList = [item];
						}
					},
					fail(err) {
						console.log('初始化蓝牙失败')
						console.error(err)
						uni.showToast({
							icon: 'none',
							title: '请确保系统蓝牙是打开状态'
						})
					}
				})
			},
			//清除蓝牙表中的uuid
			clearIptA() {
				this.serviceUuid = ""
			},
			//清除监听特定值的
			clearIptB() {
				this.eigenvalue = ""
			},
			//发送内容的输入框
			clearIptC() {
				this.sendMessage = ""
			}
		},
		onLoad() {

		}
	}
</script>

<style>
	.box {
		width: 100%;
		height: 500rpx;
		box-sizing: border-box;
		margin-bottom: 8rpx;
		border: 1px solid rgb(138, 145, 159);
	}

	.item {
		box-sizing: border-box;
		padding: 10rpx;
		border-bottom: 1px solid gray;
	}

	/* button {
		margin-bottom: 20rpx;
	} */

	.btnBox {
		display: flex;
		display: flex;
		align-items: flex-end;
		flex-wrap: wrap;
	}

	.BuuetoothServiceList {
		width: 100%;
		height: 2rem;
		margin-bottom: 8rpx;
		line-height: 2rem;
		text-align: center;
		font-weight: 700;

	}

	.msg_x {
		border: 2px solid seagreen;
		width: 98%;
		margin: 10rpx auto;
		box-sizing: border-box;
		padding: 20rpx;
	}

	/*	​.msg_txt {
		margin-bottom: 20rpx;
	} */

	.clearInput {
		width: 5%;
		height: 3rem;
		line-height: 3rem;
		float: left;
		margin-left: 2%;
	}
</style>